#!/usr/bin/env bash
#
# Description: Get hardware info for AppUnlocker.exe
# based on the script from https://teddysun.com/444.html

clear
export myMail="ask@hpc4you.top"
export weChatID="hpc4you"
export currentTime=$(date "+%Y%m%d-%H%M")

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;36m'
PLAIN='\033[0m'

getHardwareInfo() {
get_opsy() {
    [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
    [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
    [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}


dashLine() {
    printf "%-70s\n" "-" | sed 's/\s/-/g'
}

machineID=$(cat /etc/machine-id)
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
tram=$( free -g | awk '/Mem/ {print $2}' )

opsy=$( get_opsy )
arch=$( uname -m )
lbit=$( getconf LONG_BIT )
kern=$( uname -r )

dashLine
echo -e "CPU\t$cname"
echo -e "Cores\t$cores"
echo -e "Memory\t$tram GB"
echo -e "OS\t$opsy"
echo -e "Arch\t$arch ($lbit Bit)"
echo -e "Kernel\t$kern"

echo -e "ID\t${machineID}"
dashLine
echo ""
echo ""
echo -e "Disk Info:"
dashLine

if ! command -v lsblk &> /dev/null
then
    if [[ -f /etc/redhat-release ]]; then
        echo "lsblk not found. Installing on RHEL..."
        yum install util-linux -y
    elif [[ -f /etc/lsb-release ]]; then
        echo "lsblk not found. Installing on Ubuntu..."
        apt-get update
        apt-get install util-linux -y
    else
        echo "Unknown distribution. Cannot install lsblk."
        echo "Please install lsblk manually and try again."
        exit 1
    fi
fi

for disk in `lsblk | grep ^sd | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
for disk in `lsblk | grep ^nvme | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
dashLine
}

rm -fr *AppUnlocker-Info.dat &> /dev/null

getHardwareInfo > `hostname`-${currentTime}-AppUnlocker-Info.dat
fingerPrint=`md5sum *-AppUnlocker-Info.dat`

echo ""
echo ""
echo -e "I have created a file ${RED}`hostname`-${currentTime}-AppUnlocker-Info.dat${PLAIN} in the current folder."
echo -e "Please copy and paste the following ${BLUE}blue content${PLAIN} into the body of your email..."
echo ""
#echo -e "\t |"
#echo -e "\t |"
#echo -e "\t \u142f"
echo -e "${BLUE}The digital fingerprint information is: ${PLAIN}"
echo -e "${BLUE}${fingerPrint}${PLAIN}"
#echo -e "\t \u1431"
#echo -e "\t |"
#echo -e "\t |"
echo ""
echo -e "Upload file (${RED}`hostname`-${currentTime}-AppUnlocker-Info.dat${PLAIN}) as an attachment..."
echo -e "Then, send the email to ${GREEN}${myMail}${PLAIN} to request the AppUnlocker."
echo -e "You can also send the on-screen output and the ${RED}*.dat${PLAIN} file to ${YELLOW}${weChatID}${PLAIN} via WeChat/WeiXin/微信."
echo ""
echo ""
echo -e "The email title/subject/主题 should be ${RED}Request AppUnlocker${PLAIN}, "
echo -e "It would be best if you also pasted the ${RED}name of the file${PLAIN} you have downloaded from pan.baidu.com "
echo -e "and its md5 verification code into the body of email."
#echo ""
#echo ""
#echo -e "${RED}Caution, do not edit this file (`hostname`-AppUnlocker-Info.dat) on Windows!!!${PLAIN}"
echo ""
echo -e "The full path of the file (${RED}`hostname`-${currentTime}-AppUnlocker-Info.dat${PLAIN}) is, "
# echo -e "${GREEN}`pwd`${PLAIN}"
# echo ""
echo -e "\t ${GREEN}`pwd`/`hostname`-${currentTime}-AppUnlocker-Info.dat${PLAIN}"
# ls -h `hostname`-AppUnlocker-Info.dat
echo ""
echo -e "Transfer file(s): Linux <---> Windows PC"
echo -e "Video tutorial, ${BLUE}https://www.bilibili.com/video/BV1fJ411n7uV ${PLAIN}"
echo ""
#echo -e "By the way, this toolkit is ${RED}not free of charge${PLAIN}."
#echo ""
echo -e "Run ${GREEN}md5sum full_File_Name_From_BaiduShare${PLAIN} to get md5 verification code. "
echo ""
echo ""
echo "Good Luck."